package github.kawaiior.yuanchuxiuzhen.common.entity.addon.model;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import github.kawaiior.yuanchuxiuzhen.common.entity.addon.BoneWolfEntity;
import github.kawaiior.yuanchuxiuzhen.util.RenderUtil;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.util.math.MathHelper;

public class BoneWolfEntityModel extends EntityModel<BoneWolfEntity> {

    private final ModelRenderer body;

    private final ModelRenderer weiba;

    private final ModelRenderer rightleg;

    private final ModelRenderer leftleg;

    private final ModelRenderer rightarm;

    private final ModelRenderer leftarm;

    private final ModelRenderer head;

    public BoneWolfEntityModel(){
        this.textureWidth = 64;
        this.textureHeight = 32;
        this.body = new ModelRenderer(this);
        this.body.setRotationPoint(-0.5F, 19.5F, -3.0F);
        RenderUtil.setRotationAngle(this.body, 1.5708F, 0.0F, 0.0F);
        addBox(this.body, 21, 0, -3.5F, -4.5F, 3.0F, 8, 6, 7, 0.0F, false);
        addBox(this.body, 18, 14, -2.5F, 1.5F, 3.0F, 6, 9, 6, 0.0F, false);
        this.weiba = new ModelRenderer(this);
        this.weiba.setRotationPoint(0.5F, 10.5F, 8.0F);
        RenderUtil.setRotationAngle(this.weiba, -0.5236F, 0.0F, 0.0F);
        this.body.addChild(this.weiba);
        addBox(this.weiba, 9, 18, -1.0F, 0.0F, -1.0F, 2, 8, 2, 0.0F, false);
        this.rightleg = new ModelRenderer(this);
        this.rightleg.setRotationPoint(-2.0F, 16.0F, 6.0F);
        addBox(this.rightleg, 0, 18, -1.0F, 0.0F, -1.0F, 2, 8, 2, 0.0F, false);
        this.leftleg = new ModelRenderer(this);
        this.leftleg.setRotationPoint(2.0F, 16.0F, 6.0F);
        addBox(this.leftleg, 0, 18, -1.0F, 0.0F, -1.0F, 2, 8, 2, 0.0F, false);
        this.rightarm = new ModelRenderer(this);
        this.rightarm.setRotationPoint(-2.0F, 16.0F, -5.0F);
        addBox(this.rightarm, 0, 18, -1.0F, 0.0F, -1.0F, 2, 8, 2, 0.0F, false);
        this.leftarm = new ModelRenderer(this);
        this.leftarm.setRotationPoint(2.0F, 16.0F, -5.0F);
        addBox(this.leftarm, 0, 18, -1.0F, 0.0F, -1.0F, 2, 8, 2, 0.0F, false);
        this.head = new ModelRenderer(this);
        this.head.setRotationPoint(0.0F, 11.75F, -8.0F);
        addBox(this.head, 0, 0, -3.0F, -3.75F, -4.0F, 6, 6, 4, 0.0F, false);
        addBox(this.head, 0, 10, -1.5F, -0.75F, -8.0F, 3, 3, 4, 0.0F, false);
    }

    private void addBox(ModelRenderer mb, int tx, int ty, float x, float y, float z, float width, float height, float depth, float delta, boolean mirrorIn){
        ModelRenderer mr = new ModelRenderer(this);
        mb.addChild(mr);
        mr.setTextureOffset(tx, ty).addBox(x,y,z,width,height,depth,delta,mirrorIn);
    }

    @Override
    public void setRotationAngles(BoneWolfEntity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
        this.head.rotateAngleY = netHeadYaw / 57.295776F;
        this.head.rotateAngleX = headPitch / 57.295776F;
        this.rightleg.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
        this.rightarm.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
        this.leftleg.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
        this.leftarm.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
    }

    @Override
    public void render(MatrixStack matrixStackIn, IVertexBuilder bufferIn, int packedLightIn, int packedOverlayIn, float red, float green, float blue, float alpha) {
        this.body.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightleg.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.leftleg.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightarm.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.leftarm.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.head.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
    }
}
